package it.koutifaris.travianmap.info;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.util.Log;

class TroopsLoader {

	private static final String TROOPS_FILE = "troops.txt";
	private static final String TAG = TroopsLoader.class.getName();

	private final TroopFileParser parser;

	public TroopsLoader() {
		parser = new TroopFileParser();
	}

	public void load(OnTroopHandler handler) {
		try {
			InputStream troopsStream = getClass().getResourceAsStream(TROOPS_FILE);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(troopsStream));

			String line;
			while ((line = bufferedReader.readLine()) != null) {
				if (!parser.skipLine(line)) {
					Troop result = parser.parseLine(line);
					handler.onTroop(result);
				}
			}

			bufferedReader.close();
		} catch (Exception e) {
			Log.e(TAG, "Error while loading troops data: " + e);
			throw new RuntimeException(e);
		}
	}

	interface OnTroopHandler {
		void onTroop(Troop troop);
	}
}
